#include <iostream>
#include <string.h>

using namespace std;

const int N = 6;

int prime[] = {2, 3, 5, 7, 11, 13, 17};
int mark[20];
int a[20];

bool Prime(int hs)
{
    for (int i = 0; i < 7; ++i)
    {
        if (prime[i] == hs)
        {
            return true;
        }
    }
    return false;
}

void dfs(int depth)
{
    if (depth > 1)
    {
        if (!Prime(a[depth]+a[depth-1]))
        {
            return;
        }
    }
    if (depth == N)
    {
        if (!Prime(a[depth]+a[1]))
        {
            return;
        }
        for (const int& i : a)
        {
            cout << i << " ";
        }
        cout << endl;
        return;
    }

    for (int i = 2; i <= 6; ++i)
    {
        if(0 == mark[i])
        {
            mark[i] = 1;
            a[depth+1] = i;
            dfs(depth+1);
            mark[i] = 0;
        }
    }
}

int main()
{
    a[1] = 1;
    memset(&mark, 0, sizeof(mark));
    mark[1] = 1;
    dfs(1);
    return 0;
}
